import ButtonModel from "../../schema/button";
import type Typings from "../../typings";

// Mongoose 类的实例
import MongooseController from "../mongoose/mongoose";
const MongooseIns = new MongooseController(ButtonModel);

// response 响应类型类的实例
import ResController from "../res/res";
const ResIns = new ResController();

class ButtonController {
    constructor() { }

    async list(req: Typings.Express.Request, res: Typings.Express.Response) {
        try {
            let { page = 1, size = 10 } = req.body || {};
            page = Number(page);
            size = Number(size);

            const query = {};

            const total = await MongooseIns.Count(query);
            const buttons: Typings.Button.props[] = await MongooseIns.Find(query)
                .skip(size * (page - 1))
                .limit(size)
                .sort({
                    createdTime: -1,
                });

            const data = {
                page,
                size,
                list: buttons,
                total,
            };

            ResIns.Success(res, data);
        } catch (err) {
            ResIns.Database(res, err);
        }
    }

    // 新增
    async add(req: Typings.Express.Request, res: Typings.Express.Response) {
        try {
            const { parentKey, title, key } = req.body || {};

            const buttons: Typings.Button.props[] = await MongooseIns.Find({
                key,
            });

            if (buttons.length) {
                ResIns.isExist(res);
                return;
            }

            await MongooseIns.SaveOne(req, {
                parentKey,
                title,
                key
            });

            ResIns.Success(res);
        } catch (err) {
            ResIns.Database(res, err);
        }
    }

    // 更新
    async update(req: Typings.Express.Request, res: Typings.Express.Response) {
        try {
            const { id, parentKey, title, key } = <Partial<Typings.Button.props>>req.body || {};

            if (!id) {
                ResIns.paramsWrong(res);
                return;
            }

            const updateContent: Partial<Typings.Button.props> = {};

            updateContent.parentKey = parentKey;
            updateContent.title = title;
            updateContent.key = key;

            await MongooseIns.UpdateOne({ id }, updateContent);
            ResIns.Success(res);
        } catch (err) {
            ResIns.Database(res, err);
        }
    }

    // 查看
    async check(req: Typings.Express.Request, res: Typings.Express.Response) {
        try {
            const { id } = req.query || {};

            if (!id) {
                ResIns.paramsWrong(res);
                return;
            }

            // 查询
            const buttons: Typings.Button.props[] = await MongooseIns.Find({
                id,
            });

            if (!buttons.length) {
                // 不存在
                ResIns.isNotExist(res);
                return;
            }

            // 存在时
            ResIns.Success(res, buttons[0]);
        } catch (err) {
            ResIns.Database(res, err);
        }
    }

    // 删除
    async remove(req: Typings.Express.Request, res: Typings.Express.Response) {
        try {
            let { id } = req.query || {};

            if (!id) {
                ResIns.paramsWrong(res);
                return;
            }

            // 删除
            await MongooseIns.RemoveOne({ id });

            ResIns.Success(res);
        } catch (err) {
            ResIns.Database(res, err);
        }
    }
}

export default ButtonController